<template>
  <div class="app">
    <img :src="login_mobile" class="login_bg" />
    <div v-if="!loading" class="logIn">
      <div id="btn_pass">
        <p class="lg_title">用户登录</p>
        <div class="login_ipt">
          <img :src="logo" class="logo" />
          <el-form
            :inline="true"
            :model="loginForm"
            class="demo-form-inline"
            ref="loginFormRef"
            :rules="loginFormRules"
          >
            <el-form-item prop="userName">
              <el-input
                prefix-icon="el-icon-user"
                v-model="loginForm.userName"
                placeholder="请输入用户名"
                class="ipt"
              ></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                prefix-icon="el-icon-lock"
                v-model="loginForm.password"
                type="password"
                placeholder="请输入密码"
                class="ipt"
              ></el-input>
            </el-form-item>
          </el-form>
          <el-button type="primary" @click="login" id="btn">登录</el-button>
        </div>
      </div>
    </div>
    <div class="animate" v-else>
      <img src="http://m.hhkj-life.com/wechatwebsite/images/a.gif" alt />
    </div>
  </div>
</template>

<script>
import qs from "qs";
import logo from "@/assets/logo.png";
import login_mobile from "@/assets/login_mobile.png";
export default {
  data() {
    return {
      logo,
      login_mobile,
      loginForm: {
        userName: "",
        password: ""
      },
      loading: false,
      // 这是表单的验证规则对象
      loginFormRules: {
        // 验证用户名是否合法
        userName: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
          { min: 3, max: 15, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          { min: 6, max: 15, message: "长度在 6 到 15 个字符", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    let con = localStorage.getItem("userAccount");
    if (con) {
      this.loginForm = JSON.parse(con);
    }
  },
  methods: {
    login() {
      this.$refs.loginFormRef.validate(valid => {
        if (!valid) return;
        var data = new FormData();
        this.loading = true;
        data.append("userName", this.loginForm.userName);
        data.append("password", this.loginForm.password);
        this.$http
          .post("/index/loginCheck.do", qs.stringify(this.loginForm))
          .then(res => {
            localStorage.setItem(
              "userAccount",
              JSON.stringify({
                userName: this.loginForm.userName,
                password: this.loginForm.password
              })
            );
            this.loading = false;
            this.$store.commit("uId", res.data.message.member);
            console.log(res.data.message.member);
            if (res.status == "200") {
              this.$router.push("/home");
            }
          });
      });
    }
  }
};
</script>

<style lang="stylus">
.app {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url('../assets/images/login-bg.png');
  background-size: 100% 100%;
  width: 100%;
  height: 100%;

  #login_img img {
    position: absolute;
    width: 45%;
    top: 18px;
    left: 20px;
    height: 95%;
  }

  #btn_pass p {
    font-size: 1.8rem;
    position: relative;
    top: 50px;
    letter-spacing: 0.2rem;
    font-family: '黑体';
    color: #fff;
  }

  .login_ipt {
    width: 300px;
    margin: auto;
    position: relative;
    top: 100px;

    .el-input {
      width: 300px;
      background-color: rgb(14, 87, 166);
      border-radius: 5px;
    }

    [class*=' el-icon-'], [class^=el-icon-] {
      color: rgb(74, 171, 254);
      font-weight: bold !important;
    }

    .el-input__prefix, .el-input__suffix {
      color: rgb(74, 171, 254);
      font-size: 1rem !important;
      font-weight: bold !important;
    }

    .el-input__inner {
      border: solid 2px rgb(14, 119, 201);
      background-color: transparent;
      color: white;
      border-radius: 5px;
    }

    .btn {
      display: flex;
      justify-content: center;
    }
  }

  #btn {
    width: 300px;
    position: relative;
    top: 25px;
  }
}
</style>
<style>
.login_bg,
.logo {
  display: none;
}
.animate {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #ece8de !important;
}
.animate img {
  width: 500px;
}
.logIn {
  width: 400px;
  margin: 0 auto;
  height: 360px;
  background-image: url("../assets/images/login-border.jpg");
  background-size: 100% 100%;
  text-align: center;
  position: relative;
}

@media screen and (max-width: 720px) {
  .login_bg {
    display: block;
    width: 100vw;
    height: 100vh;
    position: absolute;
  }
  #login_img {
    display: none;
  }

  .lg_title {
    display: none;
  }

  .logIn {
    width: 100vw;
    margin: 0 auto;
    height: 500px;
    position: relative;
    display: flex;
    align-items: center;
    background-image: none;
  }
  .app .login_ipt {
    margin-top: -10%;
    width: 100vw;
    position: static;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
  }
  .app #btn {
    margin-left: 0;
    background: #2685fe;
    border-radius: 20px;
    padding: 5px 0;
    width: 200px !important;
    height: 40px;
  }
  .demo-form-inline {
    display: flex;
    flex-direction: column;
    justify-content: center;
  }
  .logo {
    display: block;
    width: 40%;
    padding-bottom: 15%;
  }
}
</style>

